package com.codechuan.didi_web.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class Demo {
    /**
     * @param url 访问地址
     *  @param param 需要传输参数参数；对象可以通过json转换成String
     * @param header header 参数；可以通过下面工具类将string类型转换成map
     * @return 返回网页返回的数据
     */
    public static String sendPost(String url, String param, Map<String, String> header) throws UnsupportedEncodingException, IOException {
        OutputStreamWriter out;
        BufferedReader in = null;
        String result = "";
        URL realUrl = new URL(url);
        // 打开和URL之间的连接
        HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
        //设置超时时间
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(15000);
        // 设置通用的请求属性
        if (header!=null) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                conn.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
        conn.setRequestMethod("POST");
        conn.addRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 发送POST请求必须设置如下两行
        conn.setDoOutput(true);
        conn.setDoInput(true);
        // 获取URLConnection对象对应的输出流
        out = new OutputStreamWriter( conn.getOutputStream(),"UTF-8");// utf-8编码
        // 发送请求参数
        out.write(param);

        // flush输出流的缓冲
        out.flush();
        // 定义BufferedReader输入流来读取URL的响应
        in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf8"));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
        if(out!=null){
            out.close();
        }
        if(in!=null){
            in.close();
        }
        return result;
    }

    public static void main(String[] args) {
        try {
            Map<String, String> header = new HashMap<String, String>();
            header.put("Host", " epassport.diditaxi.com.cn");
            header.put("secdd-authentication", "");
            header.put("charset", "utf-8");
            header.put("mpxlogin-ver"," 5.4.30");
            header.put("didi-header-rid","57034054d8768da8add79190d29c7476");
            header.put("User-Agent"," Mozilla/5.0 (Linux; Android 12; Pixel 3 Build/SP1A.210812.016.C2; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/122.0.6261.120 Mobile Safari/537.36 XWEB/1220067 MMWEBSDK/20240404 MMWEBID/3880 MicroMessenger/8.0.49.2600(0x28003156) WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 MiniProgramEnv/android");
            header.put("content-type"," application/x-www-form-urlencoded");
            header.put("secdd-challenge"," 3|v1.1.0||||||");
            header.put("didi-header-hint-content"," {\"lang\":\"zh-CN\",\"Cityid\":173}");
            header.put("Referer"," https://servicewechat.com/wxaf35009675aa0b2a/866/page-frame.html");

            String param = "lang=zh-CN&token=&access_key_id=9&appversion=6.8.50&channel=1100000004&_ds=&xpsid=016f554a5cbdbfdf7d670ae3dc14cdcf&xpsid_root=016f554a5cbdbfdf7d670ae3dc14cdcf&q=%7B%22api_version%22:%221.0.1%22,%22appid%22:35009,%22role%22:1,%22extra_info%22:%7B%22channel%22:1100000004%7D,%22device_name%22:%22Pixel%203%22,%22sec_session_id%22:%22Ny3d38nmxKxTyUVdBHzEB2HLTUdH8Sl661KCqZvgeewleJZUedUiuxPw98IptM%22,%22policy_id_list%22:[50001289],%22policy_name_list%22:[],%22ddfp%22:%227348f769659c3e3c860adbeb854a9b8e%22,%22lang%22:%22zh-CN%22,%22wsgenv%22:%22%22,%22cell%22:%2217623630703%22,%22country_calling_code%22:%22%2B86%22,%22code_type%22:0,%22scene%22:1%7D";
            String s = sendPost("https://epassport.diditaxi.com.cn/passport/login/v5/codeMT?&wsgsig=dd04-MhzdIRpg6nYhKrtAogSHt4oK17clmdgJyfT9X7QeaN0xIWnBGTCEWCv2k6qKsnScuMf70BvGi9D8jgRR7IsLrzLIENjmJAQxProRZyGzFsDKmF4l0IFZXiD2byEMr0nLv2T6F7zFNBDqnEv%2BuSI2DzcO04bu", param, header);
            System.out.println(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}


